package com.qfedu.yanglao20admin.plancase.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.plancase.CaseInstruct;
import com.qfedu.yanglao20admin.plancase.dao.CaseInstructDao;
import com.qfedu.yanglao20admin.plancase.dao.CaseListDao;
import com.qfedu.yanglao20admin.plancase.param.CaseListParam;
import com.qfedu.yanglao20admin.plancase.param.CaseListVOParam;
import com.qfedu.yanglao20admin.plancase.service.CaseListService;
import com.qfedu.yanglao20admin.plancase.vo.CaseListVO;
import com.qfedu.yanglao20admin.plancase.vo.LookCaseListVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2023/4/23 18:07
 */
@Service
public class CaseListServiceImpl implements CaseListService {

    @Resource
    private CaseListDao caseListDao;

    @Resource
    private CaseInstructDao caseInstructDao;


    @Override
    public Page<CaseListVO> pageCaseList(CaseListParam param) {
        // 计算偏移量
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<CaseListVO> page = new Page<>(param.getPageNum(), param.getPageSize());

        List<CaseListVO> caseListVOS = caseListDao.selectList(param);


        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = caseListDao.selectCount(null);
        page.setTotal(aLong);

        page.setRecords(caseListVOS);

        return page;
    }

    @Override
    public void deleteById(Integer id) {
        caseListDao.deleteById(id);
    }

    @Override
    public LookCaseListVO selectCaseVOById(Integer id) {
        return caseListDao.selectCaseVOById(id);
    }

    @Override
    public void insertCaseList(CaseListVOParam caseListVOParam) {


        if (caseListVOParam.getCaseName() == null || caseListVOParam.getCaseName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写预案名称");
        }
        caseListDao.insertCaseList(caseListVOParam);

        List<CaseInstruct> caseInstructList = caseListVOParam.getCaseInstructList();
        for (CaseInstruct c :
                caseInstructList) {
            c.setListId(caseListVOParam.getId());
        }

        caseInstructDao.insertAll(caseInstructList);
    }

    @Override
    public void updateCaseList(CaseListVOParam caseListVOParam) {

        if (caseListVOParam.getCaseName() == null || caseListVOParam.getCaseName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写预案名称");
        }


        caseListDao.updateCaseListById(caseListVOParam);

        List<CaseInstruct> caseInstructList = caseListVOParam.getCaseInstructList();


        caseInstructDao.updateAll(caseListVOParam.getCaseInstructList());
    }
}
